<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script src="./layui/layui.js"></script>
    <script src="./common.js"></script>
    <style>
        form {
            background-color: wheat;
            width: 200px;
            height: 400px;
            margin: 0 auto;
            padding-left: 10px;
        }
        input {
            outline: none;
        }
    </style>
</head>

<body>
    <form class="layui-form" action="">
        <h2>登录页面</h2>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="title" required lay-verify="required" placeholder="请输入手机号" autocomplete="off"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码框</label>
            <div class="layui-input-inline">
                <input type="password" name="password" required lay-verify="required" placeholder="请输入密码"
                    autocomplete="off" class="layui-input">
            </div>
        </div>
        <br>
        <div class="layui-form-item">
            <label class="layui-form-label">七天免登录</label>
            <div class="layui-input-block">
                <input type="checkbox" name="switch" lay-skin="switch" lay-text="switch">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="demo1">登录</button>
                <button type="reset" class="layui-btn layui-btn-primary">重新输入</button>
            </div>
        </div>
    </form>

    <script>
        document.querySelector("button").onclick = function () {
            // 获取元素
            let mobile = document.querySelector('input[name="title"]').value;
            let password = document.querySelector('input[name="password"]').value;
            // 获取到是否需要记住密码
            let isRemember = document.querySelector('input[name="switch"]').checked;

            let xhr = new XMLHttpRequest;
            xhr.open('POST', 'http://phpclub.org.cn/edu/server/login.php');

            //    设置请求头
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send(`mobile=${mobile}&password=${password}`);

            xhr.onload = () => {
                let response = JSON.parse(xhr.responseText);
                console.log(response);
                // 服务端返回的是JSON格式必定存在code msg data信息
                if (response.code != 100) {
                    // console.log(1111);
                    // layer.msg属于layui框架内置所封装的功能
                    layer.msg(response.msg, {
                        icon: 5
                    });

                } else {
                    // location.href = location.href = ""
                    // 如果需要存储一组数据 cookie只支持字符串格式
                    // 所以使用JSON格式转换
                    let time=0;
                    if(isRemember){
                        time=7*24*3600;

                    }
                    setCookie("userInfo",JSON.stringify(response.data),time)
                    // console.log(response.data);
                    setCookie('userInfo', JSON.stringify(response.data))
                    location.href = "index.html" 
                }



            }
        }
    </script>

</body>

</html>